package com.ipay.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 时间工具类
 *
 * @author
 * @version 1.0
 * @time 2017/5/2
 */
public class TimeUtils {

    /**
     * 获取昨天日期
     *
     * @param date
     * @return
     */
    public static String getYesterday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String d = sdf.format(date);
        return d;
    }

    /**
     * 获取间隔几分钟后的时间
     *
     * @param date
     * @param minute
     * @return
     */
    public static String getTimeByMinute(Date date, int minute) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

    }

    /**
     * 获取间隔几分钟后的时间
     *
     * @param date
     * @return
     */
    public static String getTimeByDate(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

    }

    /**
     * 获取间隔几个小时后的时间
     *
     * @param date
     * @param hour
     * @return
     */
    public static String getTimeByhour(Date date, int hour) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

    }

    /**
     * 获取间隔几天后的日期
     *
     * @param date
     * @param day
     * @return
     */
    public static String getTimeByDay(Date date, int day) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, day);
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

    }

    /**
     * 获取间隔几前的日期
     *
     * @param day
     * @return
     */
    public static Date getTimeByAfterDay(int day) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -day);
        return calendar.getTime();

    }

    /**
     * 时间比较函数，开始时间大于等于结束时间返回false
     *
     * @param starttime
     * @param endtime
     * @return
     */
    public static boolean compare_date(String starttime, String endtime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dt1 = df.parse(starttime);
            Date dt2 = df.parse(endtime);
            if (dt1.getTime() >= dt2.getTime()) {
                return false;
            } else if (dt1.getTime() < dt2.getTime()) {
                return true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return true;
    }

    /**
     * 获取时间间隔longtime值
     *
     * @return
     */
    public static List gettimeinterval(String str) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date d = null;
        try {
            d = sdf1.parse(str);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        List list = new ArrayList();
        if(d!=null) {
            String datestr = getYesterday(d) + " 00:00:00";
            String endtime = getYesterday(d) + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date = sdf.parse(datestr);
                list.add(date.getTime());
                while (true) {
                    String nexttime = getTimeByhour(date, 1);
                    date = sdf.parse(nexttime);
                    list.add(date.getTime());
                    if (!compare_date(nexttime, endtime)) {
                        break;
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 获取任意时间间隔longtime值
     *
     * @return
     */
    public static List gettimeinterval(String starttime, int hour) {
        List list = new ArrayList();
        List list1 = new ArrayList();
        String datestr = starttime + " 00:00:00";
        list1.add(datestr);
        String endtime = starttime + " 23:59:59";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(datestr);
            list.add(date.getTime());
            while (true) {
                String nexttime = getTimeByhour(date, hour);
                list1.add(nexttime);
                date = sdf.parse(nexttime);
                list.add(date.getTime());
                if (!compare_date(nexttime, endtime)) {
                    break;
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(list1);
        return list;
    }

    /**
     * 获取时间间隔longtime值，天粒度
     *
     * @return
     */
    public static List getdayinterval(String startime, String endtime) {
        List list1 = new ArrayList();
        list1.add(startime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(startime);
            while (true) {
                String nexttime = getTimeByDay(date, 1);
                list1.add(nexttime);
                date = sdf.parse(nexttime);
                if (!compare_date(nexttime + " 00:00:00", endtime + " 00:00:00")) {
                    break;
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list1;
    }

    public static String gettimeBylongtimes(long longtimes) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(longtimes);
        return formatter.format(calendar.getTime());
    }

    /**
     * 获取2个日期段 日期
     *
     * @param start
     * @param end
     * @return
     * @throws ParseException
     */
    public static List<String> findDates(String start, String end) {
        List<String> lDate = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dBegin = sdf.parse(start);
            Date dEnd = sdf.parse(end);
            lDate.add(sdf.format(dBegin));
            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(dBegin);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(dEnd);
            // 测试此日期是否在指定日期之后
            while (dEnd.after(calBegin.getTime())) {
                // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                calBegin.add(Calendar.DAY_OF_MONTH, 1);
                lDate.add(sdf.format(calBegin.getTime()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return lDate;
    }

    public static void main(String[] atgs) {
        String start = "2017-09-19";
        String end = "2017-10-19";
        List<String> lDate = findDates(start, end);
        for (String date : lDate) {
            System.out.println(date);
        }

        // TimeUtils.gettime();
        //System.out.println(TimeUtils.getNextDay(new Date()));
        //System.out.println(new Date().getTime());
        //System.out.println(TimeUtils.gettimeinterval("2017-07-05", 2));
        System.out.println(gettimeBylongtimes(Long.parseLong("1504188800940")));
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //System.out.println(TimeUtils.gettimeinterval("2015-11-27 18:24:31"));

		/*
		 * Date date; try { date = sdf.parse("2017-05-01 00:00:00)"); long
		 * longDate = date.getTime(); System.out.println(longDate); date =
		 * sdf.parse("2017-05-01 23:59:59)"); longDate = date.getTime();
		 * System.out.println(longDate); } catch (ParseException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
	
			/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
				Date date = null;
				try {
					date = sdf.parse("2015-11-27 18:24:31");
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(date.getTime());*/
    }
}
